package pm.gui.components;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @author Anton Bernatski
 * @version $Revision$ $Date$
 */
public class ImageButton extends JButton {
	private BufferedImage image;

	public ImageButton(BufferedImage image) {
		this.image = image;
		init();
	}

	private void init() {
		setSize(this.image.getWidth(null), this.image.getHeight(null));
		setIcon(new ImageIcon(this.image));
		setMargin(new Insets(0, 0, 0, 0));
		setIconTextGap(0);
		setBorderPainted(false);
		setBorder(null);
		setText(null);
	}

	@Override
	public boolean contains(final int x, final int y) {
		if ((x < 0 || x > image.getWidth()) || (y < 0 || y > image.getHeight())) {
			return false;
		}
		return image.getAlphaRaster().getSample(x, y, 0) > 0;
	}
}
